import 'dart:async';
import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/http_service/http_attachment_service.dart';
import 'package:finance_app/src/widgets/bottom_sheet/attachments_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/delete_info_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/photo_preview_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';

mixin SingleAttachMixin<T> on BaseProvider<T> {
  String imagePath;
  String networkPath;

  void unFocus();

  Future<void> onAddAttachment({String title, String attachName}) async {
    unFocus();
    Completer<void> completer = Completer();
    obtainContext?.call((context) async {
      final result = await showAttachmentsBottomSheet(context,
          title: title, attachName: attachName);
      if (result != null) {
        imagePath = result;
        update();
        completer.complete(null);
      }
    });
    await completer.future;
  }

  void onSelectFile(String value,
      {VoidCallback onChooseAgain,
      String againText,
      VoidCallback onRemove,
      bool canDelete = true,
      OcrAttachmentEntity attachments}) {
    unFocus();

    obtainContext?.call((context) async {
      bool isSelect = await showPhotoPreviewBottomSheet(context,
          (value?.isNotEmpty ?? false) ? File(value) : null, attachments,
          canDelete: canDelete,
          onChooseAgain: onChooseAgain,
          onRemove: onRemove,
          chooseAgainText: againText);
      if (isSelect == true) {
        await showDeleteAttachmentInfoBottomSheet(context, onCancel: () {
          Navigator.of(context).pop();
        }, onDelete: () {
          Navigator.of(context).pop();
          imagePath = null;
          update();
        });
      }
    });
  }

  Future<OcrAttachmentEntity> uploadFiles(bool compressFile) async {
    OcrAttachmentEntity attachment;
    obtainContext?.call((context) {
      BaseConfig.dialogBase
          .showLoadingDialog(context: context, text: S.current.loading);
    });
    if (imagePath != null) {
      if (compressFile) {
        await compressImage(imagePath);
      }
      await post(
          HttpAttachmentService.createAttachment(
            imagePath,
          ), onSuccess: (result) {
        attachment = getEntityFromResult<OcrAttachmentEntity>(result);
      });
      if (attachment == null) {
        showToast(S.current.file_upload_failure);
        obtainContext?.call((context) {
          Navigator.of(context).pop();
        });
        return null;
      }
    }
    return attachment;
  }

  Future<void> compressImage(String originalFilePath) async {
    File file = File(originalFilePath);
    if (file.existsSync()) {
      print("before compress");
      print(file.lengthSync());
      print(file.path);
      final directory = await getTemporaryDirectory();
      String filename =
          '${path.basenameWithoutExtension(originalFilePath)}_compress${path.extension(originalFilePath)}';
      File compressedFile = File((path.join(directory.path, filename)));
      compressedFile.createSync();
      await FlutterImageCompress.compressAndGetFile(
        file.absolute.path,
        compressedFile.path,
        quality: 80,
      );
      print("after compress");
      print(compressedFile.lengthSync());
      print(compressedFile.path);
      imagePath = compressedFile.path;
    }
  }
}
